/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scheduler;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.text.*;

/**
 *
 * @author Musikero
 */
public class InstructorWindow extends javax.swing.JFrame {

    static int realDay, realMonth, realYear, currentMonth, currentYear;
    private Vector<Vector<String>> data;
    private Vector<Vector<String>> data2;
    private Vector<String> header;   
    private Vector<String> header2;
    private static String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
    /**
     * Creates new form InstructorWindow
     */
    public InstructorWindow() throws Exception{
        DbEngine dbEngine = new DbEngine();
        data = dbEngine.getStudent();
        data2 = dbEngine.getDropped();
        header = new Vector<>();
        header.add("Id Number");
        header.add("Last Name");
        header.add("First Name");
        header.add("Middle Initial");
        header.add("Absences");
        header.add("Tardies");
        header2 = new Vector<>();
        header2.add("Id Number");
        header2.add("Last Name");
        header2.add("First Name");
        header2.add("Middle Initial");
        initComponents();
        
            
        try{
            String instructorId;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String conStr = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\\DatabaseUpdate.accdb";
            //edit the database location above: d:/student.accdb
            Connection con = DriverManager.getConnection(conStr);
            System.out.println("Connected");

            String selectStatement = "SELECT * FROM INSTRUCTOR INNER JOIN SUBJECT ON INSTRUCTOR.CLASSCODE=SUBJECT.CLASSCODE where instructor.classcode='9572'";
            
            Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet result = stmt.executeQuery(selectStatement);

            result.beforeFirst();
            while(result.next()){
            String name = (result.getString(2) + " " + result.getString(3) + " " + result.getString(4)); 
            instructorNameField.setText(name);
            }
            
            
           
        }
        catch(Exception a){
            a.printStackTrace();
        } 
        
        GregorianCalendar cal = new GregorianCalendar();
        realDay = cal.get(GregorianCalendar.DAY_OF_MONTH);
        realMonth = cal.get(GregorianCalendar.MONTH);
        realYear = cal.get(GregorianCalendar.YEAR);
        currentMonth = realMonth;
        currentYear = realYear;
        
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
        WelcomeLabel = new javax.swing.JLabel();
        SubjectLabel = new javax.swing.JLabel();
        InstructorSubjectField = new javax.swing.JTextField();
        TabbedPaneLabel = new javax.swing.JLabel();
        InstructorExit = new javax.swing.JButton();
        InstructorChangePassword = new javax.swing.JButton();
        DateLabel = new javax.swing.JLabel();
        CheckAttendanceButton = new javax.swing.JButton();
        panelCalendar = new javax.swing.JPanel();
        labelMonth = new javax.swing.JLabel();
        buttonNext = new javax.swing.JButton();
        buttonPrev = new javax.swing.JButton();
        labelYear = new javax.swing.JLabel();
        scrollTableCalendar = new javax.swing.JScrollPane();
        tableCalendar = new javax.swing.JTable();
        comboYear = new javax.swing.JComboBox();
        refreshButton = new javax.swing.JLayeredPane();
        InstructorTabbedPane = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        instructorNameField = new javax.swing.JTextField();
        Refresh = new javax.swing.JButton();
        submitButton = new javax.swing.JButton();
        dateLabel = new javax.swing.JLabel();
        dateField = new javax.swing.JLabel();
        InstructorMenuBar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        jRadioButtonMenuItem1.setSelected(true);
        jRadioButtonMenuItem1.setText("jRadioButtonMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        WelcomeLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        WelcomeLabel.setText("Welcome:");

        SubjectLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        SubjectLabel.setText("Subject:");

        InstructorSubjectField.setEditable(false);
        InstructorSubjectField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        InstructorSubjectField.setText("9572 Programming Applications");
        InstructorSubjectField.setBorder(null);
        InstructorSubjectField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InstructorSubjectFieldActionPerformed(evt);
            }
        });

        TabbedPaneLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        TabbedPaneLabel.setText("Class Cards");

        InstructorExit.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        InstructorExit.setText("Exit");
        InstructorExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InstructorExitActionPerformed(evt);
            }
        });

        InstructorChangePassword.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        InstructorChangePassword.setText("Change password");
        InstructorChangePassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InstructorChangePasswordActionPerformed(evt);
            }
        });

        DateLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        DateLabel.setText("Date:");

        CheckAttendanceButton.setText("Check Attendance");
        CheckAttendanceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CheckAttendanceButtonActionPerformed(evt);
            }
        });

        panelCalendar.setBorder(javax.swing.BorderFactory.createTitledBorder("Calendar"));

        labelMonth.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelMonth.setText("January");

        buttonNext.setText(">>");

        buttonPrev.setText("<<");

        labelYear.setText("Change Year:");

        tableCalendar.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableCalendar.setRowHeight(30);
        tableCalendar.getTableHeader().setReorderingAllowed(false);
        scrollTableCalendar.setViewportView(tableCalendar);

        for (int i = realYear - 100; i <= realYear + 100; i++){
            comboYear.addItem(String.valueOf(i));
        }

        javax.swing.GroupLayout panelCalendarLayout = new javax.swing.GroupLayout(panelCalendar);
        panelCalendar.setLayout(panelCalendarLayout);
        panelCalendarLayout.setHorizontalGroup(
            panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCalendarLayout.createSequentialGroup()
                .addComponent(buttonPrev)
                .addGap(147, 147, 147)
                .addComponent(labelMonth)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(buttonNext))
            .addGroup(panelCalendarLayout.createSequentialGroup()
                .addComponent(labelYear)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(comboYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(scrollTableCalendar)
        );
        panelCalendarLayout.setVerticalGroup(
            panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCalendarLayout.createSequentialGroup()
                .addGroup(panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonNext)
                    .addGroup(panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(buttonPrev)
                        .addComponent(labelMonth)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollTableCalendar, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelYear)
                    .addComponent(comboYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        buttonNext.addActionListener(new buttonNext_Action());
        buttonPrev.addActionListener(new buttonPrev_Action());
        refreshCalendar (realMonth, realYear);

        comboYear.addActionListener(new comboYear_Action());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            data, header
        ));
        jScrollPane1.setViewportView(jTable1);

        InstructorTabbedPane.addTab("Students", jScrollPane1);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            data2, header2
        ));
        jScrollPane2.setViewportView(jTable2);

        InstructorTabbedPane.addTab("Students w/ Dropped Class cards", jScrollPane2);

        InstructorTabbedPane.setBounds(0, 0, 770, 238);
        refreshButton.add(InstructorTabbedPane, javax.swing.JLayeredPane.DEFAULT_LAYER);

        instructorNameField.setEditable(false);

        Refresh.setText("Refresh");
        Refresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RefreshActionPerformed(evt);
            }
        });

        submitButton.setText("Submit Classcard");
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });

        dateLabel.setText(date);

        dateField.setText(date);

        jMenu1.setText("File");
        InstructorMenuBar.add(jMenu1);

        jMenu2.setText("Edit");
        InstructorMenuBar.add(jMenu2);

        setJMenuBar(InstructorMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(InstructorChangePassword)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CheckAttendanceButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Refresh)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(submitButton)
                        .addGap(302, 302, 302)
                        .addComponent(InstructorExit))
                    .addComponent(refreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 776, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(instructorNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(TabbedPaneLabel)
                                    .addComponent(WelcomeLabel)
                                    .addComponent(SubjectLabel)
                                    .addComponent(DateLabel)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(InstructorSubjectField, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(dateField))))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(panelCalendar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 377, Short.MAX_VALUE)
                    .addComponent(dateLabel)
                    .addGap(0, 378, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(panelCalendar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 244, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(WelcomeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(instructorNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(SubjectLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(InstructorSubjectField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DateLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dateField)
                        .addGap(60, 60, 60)
                        .addComponent(TabbedPaneLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(refreshButton, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(InstructorChangePassword)
                    .addComponent(InstructorExit)
                    .addComponent(CheckAttendanceButton)
                    .addComponent(Refresh)
                    .addComponent(submitButton))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 243, Short.MAX_VALUE)
                    .addComponent(dateLabel)
                    .addGap(0, 244, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void InstructorSubjectFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InstructorSubjectFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_InstructorSubjectFieldActionPerformed

    private void InstructorExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InstructorExitActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_InstructorExitActionPerformed

    private void InstructorChangePasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InstructorChangePasswordActionPerformed
        // TODO add your handling code here:
        ChangePassword changePass = new ChangePassword();
        changePass.setVisible(true);
    }//GEN-LAST:event_InstructorChangePasswordActionPerformed

    private void CheckAttendanceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CheckAttendanceButtonActionPerformed
        try {
            // TODO add your handling code here:
            AttendanceChecking name = new AttendanceChecking();
            name.setVisible(true);
        } catch (Exception ex) {
            Logger.getLogger(InstructorWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_CheckAttendanceButtonActionPerformed

    private void instructorNameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_instructorNameFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_instructorNameFieldActionPerformed

    private void RefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RefreshActionPerformed
        // TODO add your handling code here:
        jTable1.updateUI();
        jTable2.updateUI();
    }//GEN-LAST:event_RefreshActionPerformed

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
        // TODO add your handling code here:
        try{
            SubmitClasscard submit = new SubmitClasscard();
            submit.setVisible(true);
        }
        catch(Exception a){
            a.printStackTrace();
        }
    }//GEN-LAST:event_submitButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String [] args) {
        
        System.out.println("Print debugging");
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(InstructorWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(InstructorWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(InstructorWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(InstructorWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    new InstructorWindow().setVisible(false);
                }
                catch(Exception a){
                    a.printStackTrace();
                }
            }
        });
        System.out.println("Print Debuging");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CheckAttendanceButton;
    private javax.swing.JLabel DateLabel;
    private javax.swing.JButton InstructorChangePassword;
    private javax.swing.JButton InstructorExit;
    private javax.swing.JMenuBar InstructorMenuBar;
    private static javax.swing.JTextField InstructorSubjectField;
    private javax.swing.JTabbedPane InstructorTabbedPane;
    private javax.swing.JButton Refresh;
    private javax.swing.JLabel SubjectLabel;
    private javax.swing.JLabel TabbedPaneLabel;
    private javax.swing.JLabel WelcomeLabel;
    static javax.swing.JButton buttonNext;
    static javax.swing.JButton buttonPrev;
    static javax.swing.JComboBox comboYear;
    private javax.swing.JLabel dateField;
    private javax.swing.JLabel dateLabel;
    private static javax.swing.JTextField instructorNameField;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private static javax.swing.JTable jTable1;
    private static javax.swing.JTable jTable2;
    static javax.swing.JLabel labelMonth;
    static javax.swing.JLabel labelYear;
    static javax.swing.JPanel panelCalendar;
    private javax.swing.JLayeredPane refreshButton;
    static javax.swing.JScrollPane scrollTableCalendar;
    private javax.swing.JButton submitButton;
    static javax.swing.JTable tableCalendar;
    // End of variables declaration//GEN-END:variables

    public static void refreshCalendar(int month, int year){
        String[] months = {"January", "February", "March", "April", "May",
                "June", "July", "August", "September", "October", "November", "December"};
        int nod, som; //Number Of Days, Start Of Month

        buttonPrev.setEnabled(true); //Enable buttons at first
        buttonNext.setEnabled(true);
        if (month == 0 && year <= realYear-10){
            buttonPrev.setEnabled(false);
        } //Too early
        if (month == 11 && year >= realYear+100){
            buttonNext.setEnabled(false);
        } //Too late
        labelMonth.setText(months[month]); //Refresh the month label (at the top)
        labelMonth.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); //Re-align label with calendar
        comboYear.setSelectedItem(String.valueOf(year)); //Select the correct year in the combo box
        
        GregorianCalendar cal = new GregorianCalendar(year, month, 1);
        
        nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
        som = cal.get(GregorianCalendar.DAY_OF_WEEK);
        
        //clearTable
        for (int i=0; i<6; i++){
            for (int j=0; j<7; j++){
                tableCalendar.getModel().setValueAt(null, i, j);
            }
        }
        
        //Draw calendar
        for (int i=1; i<=nod; i++){
            int row = new Integer((i+som-2)/7);
            int column  =  (i+som-2)%7;
            tableCalendar.getModel().setValueAt(i, row, column);
        }
        
        tableCalendar.setDefaultRenderer(tableCalendar.getColumnClass(0), new tableCalendarRenderer()); //Apply renderer
    }
    
    static class tableCalendarRenderer extends DefaultTableCellRenderer{
            public Component getTableCellRendererComponent (JTable table, Object value, 
            boolean selected, boolean focused, int row, int column){
              
            super.getTableCellRendererComponent(table, value, selected, focused, row, column);
            if (column == 0 || column == 6){ //Week-end
                setBackground(new Color(255, 220, 220));
            }
            else{ //Week
                setBackground(new Color(255, 255, 255));
            }
            if (value != null){
                if (Integer.parseInt(value.toString()) == realDay && currentMonth == realMonth && 
                        currentYear == realYear){ //Today
                    setBackground(new Color(220, 220, 255));
                }
            }
            setBorder(null);
            setForeground(Color.black);
            return this; 
        }

    }
    
    static class buttonPrev_Action implements ActionListener{
        public void actionPerformed (ActionEvent e){
            if (currentMonth == 0){ //Back one year
                currentMonth = 11;
                currentYear -= 1;
            }else{ //Back one month
                currentMonth -= 1;
            }
            refreshCalendar(currentMonth, currentYear);
        }
    }
    
    static class buttonNext_Action implements ActionListener{
        public void actionPerformed (ActionEvent e){
            if (currentMonth == 11){ //Foward one year
                currentMonth = 0;
                currentYear += 1;
            }else{ //Foward one month
                currentMonth += 1;
            }
            refreshCalendar(currentMonth, currentYear);
        }
    }
    
    static class comboYear_Action implements ActionListener{
        public void actionPerformed (ActionEvent e){
            if (comboYear.getSelectedItem() != null){
                String b = comboYear.getSelectedItem().toString();
                currentYear = Integer.parseInt(b); //Get the numeric value
                refreshCalendar(currentMonth, currentYear); //Refresh
            }
        }
    }
}
